package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.deepdesigner.pojo.DtsSearchHistory;
import top.deepdesigner.service.DtsSearchHistoryService;

import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/23 星期五 18:01:27 <br/>
 */
@Service
public class DtsSearchHistoryServiceImpl extends ServiceImpl<BaseMapper<DtsSearchHistory>, DtsSearchHistory> implements DtsSearchHistoryService {
  @Override
  public Long getSearchHistoryCount() {
    QueryWrapper<DtsSearchHistory> wrapper = new QueryWrapper<>();
    wrapper.eq(true, "deleted", 0);
    return this.count(wrapper);
  }

  @Override
  public List<DtsSearchHistory> getSearchHistoryList(Integer page, Integer limit, String sort, String order, Integer userId, String keyword) {
    QueryWrapper<DtsSearchHistory> wrapper = new QueryWrapper<>();
    wrapper.eq(true, "deleted", 0)
        .eq(userId != null, "user_id", userId)
        .like(!StringUtils.isEmpty(keyword), "keyword", keyword)
        .orderBy(!StringUtils.isEmpty(sort), "asc".equalsIgnoreCase(order), sort);
    return this.page(new Page<>(page, limit), wrapper).getRecords();
  }
}
